/*
 * file_mem_map.cpp
 * map a file into memory
 * Created on: 2008-8-15
 * Author: root
 */

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	int fd;
	char *mapped_mem, * p;
	int flength = 1024;
	void * start_addr = 0;

	fd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	flength = lseek(fd, 1, SEEK_END);
	write(fd, "\0", 1); /* 在文件最后添加一个空字符，以便下面printf正常工作 */
	lseek(fd, 0, SEEK_SET);
	start_addr = 0x80000;
	mapped_mem = mmap(start_addr, flength, PROT_READ | PROT_WRITE, //允许写入
			MAP_SHARED, //允许其它进程访问此内存区域
			fd, 0);
	/* 使用映射区域. */
	printf("%s\n", mapped_mem); /* 为了保证这里工作正常，参数传递的文件名最好是一个文本文件 */
	while ((p = strstr(mapped_mem, "Hello")))
	{ /* 此处来修改文件 内容 */
		memcpy(p, "Linux", 5);
		p += 5;
	}
	close(fd);
	munmap(mapped_mem, flength);
	return 0;
}
